package org.pps.gl.client.vue;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;

import org.pps.gl.client.LoginInfo;
import org.pps.gl.client.data.NewsInfo;
import org.pps.gl.client.data.UserInfo;

import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;

/**
 * Composite pour l'affichage d'une news
 * 
 */
public class ACL_News extends Composite {
	VerticalPanel flux;
	ScrollPanel main;
	public ACL_News() {
		
		
		
		flux = new VerticalPanel();
		flux.setStyleName("profilMarge");
		Label titre = new Label("Actualité de votre réseau");
		titre.setStylePrimaryName("titre");
		flux.add(titre);
		ArrayList<String> contacts = LoginInfo.getCurrentUser().getUserInfo().getContacts();
		
		// Parcourir la liste des news
		ArrayList<NewsInfo> news = LoginInfo.getCurrentUser().getUserInfo().getNews();
		// on trie les news du plus récent
		Collections.reverse(news);
		for(NewsInfo n : news){
			switch(n.getEventType()){
				case 4 :
					UserInfo user1 = n.getOwnerInfo();
					UserInfo user2 = null;
					boolean onlyOneUser = true;
					for(UserInfo u : n.getUsers()){
						if(!user1.getEmailAddress().equals(u.getEmailAddress())){
							onlyOneUser = false;
							user2 = u;
						}
					}
					if(!onlyOneUser){
						boolean continu = true;
						Iterator<String> it = contacts.iterator();
						while(continu && it.hasNext()){
							String mail = it.next();
							if(user1.getEmailAddress().equals(mail)){
								continu = false;
							}
							else if(user2.getEmailAddress().equals(mail)){
								UserInfo tmp = user1; 
								user1 = user2;
								user2 = tmp;
								continu = false;
							}
							
						}
						
						HorizontalPanel line = new HorizontalPanel();
						line.setSpacing(5);
						
						Hyperlink hyperlink1 = new Hyperlink("","user/"+user1.getEmailAddress());
						hyperlink1.setStyleName("ficheHyper");
						hyperlink1.setStylePrimaryName("ficheHyper");
						line.add(hyperlink1);
						hyperlink1.setSize("33px", "30px");
						flux.add(line);
						line.setSize("550px", "10px");
						Label label = new Label(user1.getFirstname()+" "+user1.getLastname()+" "+n.getDescription() + " "+user2.getFirstname()+" "+user2.getLastname());
						label.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
						line.add(label);
						line.setCellVerticalAlignment(label,
								HasVerticalAlignment.ALIGN_MIDDLE);
						label.setSize("463px", "20px");
						Hyperlink hyperlink2 = new Hyperlink("","user/"+user2.getEmailAddress());
						hyperlink2.setStyleName("ficheHyper");
						hyperlink2.setStylePrimaryName("ficheHyper");
						line.add(hyperlink2);
						hyperlink2.setSize("33px", "30px");
					}
					break;
				default :
					HorizontalPanel line = new HorizontalPanel();
					line.setSpacing(5);
					
					UserInfo owner = n.getOwnerInfo();
					Hyperlink hyperlink1 = new Hyperlink("","user/"+owner.getEmailAddress());
					hyperlink1.setStyleName("ficheHyper");
					hyperlink1.setStylePrimaryName("ficheHyper");
					line.add(hyperlink1);
					hyperlink1.setSize("33px", "30px");
					flux.add(line);
					line.setSize("550px", "10px");
					Label label = new Label(owner.getFirstname()+" "+owner.getLastname()+" "+n.getDescription());
					label.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
					line.add(label);
					line.setCellHeight(label, "10px");
					line.setCellVerticalAlignment(label,
							HasVerticalAlignment.ALIGN_MIDDLE);
					label.setSize("363px", "20px");
					break;
			}
			
		}
		main = new ScrollPanel();
		main.add(flux);
		initWidget(main);
	}
}
